package com.wthink.hzgj.util

import android.R.attr.data
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import com.alibaba.fastjson.JSON
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.Constants
import com.wthink.hzgj.data.domain.NoticeMsg
import com.wthink.newMain.newBusiness.data.BusinessData
import java.lang.reflect.Type
import java.math.BigDecimal
import java.text.SimpleDateFormat


object PreferencesUtils {
    private val name = "APP_Config"
    private val prefs: SharedPreferences by lazy { BusApplication.busContext.getSharedPreferences(name, Context.MODE_PRIVATE) }

    /**
     * 获取存放数据
     * @return 值
     */
    @Suppress("UNCHECKED_CAST")
    fun getValue(key: String, default: Any): Any = with(prefs) {
        return when (default) {
            is Int -> getInt(key, default)
            is String -> getString(key, default)!!
            is Long -> getLong(key, default)
            is Float -> getFloat(key, default)
            is Boolean -> getBoolean(key, default)
            else -> throw IllegalArgumentException("SharedPreferences 类型错误")
        }
    }

    fun getStringObject(key: String, default: String = ""): String {
        return getValue(key, default) as String
    }

    fun getInt(key: String, default: Int = 0): Int {
        return getValue(key, default) as Int
    }

    fun getLong(key: String, default: Long = 0): Long {
        return getValue(key, default) as Long
    }

    fun getBoolean(key: String, default: Boolean = false): Boolean {
        return getValue(key, default) as Boolean
    }

    fun getFloat(key: String, default: Float = 0f): Float {
        return getValue(key, default) as Float
    }
    /**
     * 存放SharedPreferences
     * @param key 键
     * @param value 值
     */
    fun saveValue(key: String, value: Any) = with(prefs.edit()) {
        when (value) {
            is Long -> putLong(key, value)
            is Int -> putInt(key, value)
            is String -> putString(key, value)
            is Float -> putFloat(key, value)
            is Boolean -> putBoolean(key, value)
            else -> throw IllegalArgumentException("SharedPreferences 类型错误")
        }.apply()
    }

    /**
     * 清除
     */
    fun clear() {
        prefs.edit().clear().apply()
    }

    /**
     * 删除某Key的值
     */
    fun remove(key: String) {
        prefs.edit().remove(key).apply()
    }

    fun saveListToString(list: MutableList<NoticeMsg>){
/*
        if (list.size > 99) {
            var list: MutableList<NoticeMsg> = mutableListOf()
            val sortedByDescending = list.sortedByDescending { it.time }
            sortedByDescending.forEachIndexed { index, noticeMsg ->
                if (index < 20) {
                    list.add(noticeMsg)
                }
            }
            val toJSONString = JSON.toJSONString(list)
            saveValue(Constants.SP_NOTICEMSGLIST, toJSONString)
        } else {
            val toJSONString = JSON.toJSONString(list)
            saveValue(Constants.SP_NOTICEMSGLIST, toJSONString)
        }
*/
        val sortedByDescending = list.sortedByDescending { it.time }
        val toJSONString = JSON.toJSONString(sortedByDescending)
        saveValue(Constants.SP_NOTICEMSGLIST, toJSONString)
    }

    fun stringToList():MutableList<NoticeMsg>{
        val noticeMsgJson = getStringObject(Constants.SP_NOTICEMSGLIST)
        Log.d("noticeMsgJson", noticeMsgJson)
        return if (noticeMsgJson.contains("index")) {
            val type: Type = object : TypeToken<List<NoticeMsg?>?>() {}.type
            val list: MutableList<NoticeMsg> = Gson().fromJson(noticeMsgJson, type)
            list
        } else {
            mutableListOf<NoticeMsg>()
        }
    }

    fun getSpTime():String{
        val mix = getLong("mix")
        val nowTime : BigDecimal = BigDecimal(mix).add(BigDecimal(System.currentTimeMillis()))
        val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(nowTime)
        return  time
    }

    fun saveBusinessUpListToString(businessData: BusinessData){
        val business = getStringObject(Constants.BUSINESS_LIST)
        if (business.isEmpty() || business == null) {
            val string = ArrayList<BusinessData>()
            saveValue(Constants.BUSINESS_LIST,Gson().toJson(string))
        }

        val stringObject = getStringObject(Constants.BUSINESS_LIST)
        val listType =
            object : TypeToken<ArrayList<BusinessData?>?>() {}.type
        val list: ArrayList<BusinessData> = Gson().fromJson(stringObject, listType)

        list.add(businessData)
        saveValue(Constants.BUSINESS_LIST,Gson().toJson(list))
    }
}